package com.pospi.xstore.fragment;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.pospi.xstore.R;
import com.pospi.xstore.ui.bh.SupAutoListActivity;
import com.pospi.xstore.ui.bh.SupGoodsListActivity;
import com.pospi.xstore.ui.caigou.CgshListActivity;
import com.pospi.xstore.ui.caigou.CountsListActivity;
import com.pospi.xstore.ui.caigou.OverFlowListActivity;
import com.pospi.xstore.ui.caigou.PdListActivity;
import com.pospi.xstore.ui.caigou.ReceiveBillListActivity;
import com.pospi.xstore.ui.caigou.RefundBillListActivity;
import com.pospi.xstore.ui.cash_ui.RefundActivity;
import com.pospi.xstore.ui.cash_ui.RestOrderActivity;
import com.pospi.xstore.ui.cash_ui.ScanReceiptActivity;
import com.pospi.xstore.ui.diaob.DboCyListActivity;
import com.pospi.xstore.ui.diaob.DboOutListActivity;
import com.pospi.xstore.ui.diaob.DboReceiveListActivity;
import com.pospi.xstore.ui.diaob.PsCyListActivity;
import com.pospi.xstore.ui.diaob.PsOutListActivity;
import com.pospi.xstore.ui.diaob.PsReceiveListActivity;
import com.pospi.xstore.ui.xs.OutBillListActivity;
import com.pospi.xstore.ui.xs.SaleListActivity;
import com.pospi.xstore.util.Constant;
import com.pospi.xstore.util.TurnSize;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VipFragment extends BaseFragment implements AdapterView.OnItemClickListener {

//    @Bind(R.id.tv_pop)
//    TextView tvPop;
//    @Bind(R.id.et_vipNumber)
//    EditText etVipNumber;
//    @Bind(R.id.tv_search)
//    TextView tvSearch;
    @Bind(R.id.gridview_main)
    GridView gridview_main;

    private SimpleAdapter adapter;
    private List<Map<String, Object>> dataList;
    private Context context;

    private PopupWindow pop;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_vip, container, false);
        String[] from={"img","text"};
        int[] to={R.id.img,R.id.text};
        ButterKnife.bind(this, view);
        context = getActivity().getApplication();
        initData();
        adapter=new SimpleAdapter(context, dataList, R.layout.grid_item, from, to);
        gridview_main.setAdapter(adapter);
        gridview_main.setOnItemClickListener(this);
        return view;
    }
    private void initData() {
        //图标
        int icno[] = {R.drawable.icon_cgdd,R.drawable.icon_cgsh,R.drawable.icon_cgtc, R.drawable.icon_tbck,R.drawable.icon_tbrk,
                R.drawable.icon_xsck,R.drawable.icon_tbck,R.drawable.icon_tbrk, R.drawable.icon_xsck,
                R.drawable.icon_xsdd,R.drawable.icon_xsck,R.drawable.icon_xsth,
                R.drawable.icon_mdbh,R.drawable.icon_mdbh,R.drawable.icon_sspd,R.drawable.icon_sspd, R.drawable.icon_spzl,R.drawable.icon_spsy};
        //图标下的文字
        String name[]={
                "采购订单","采购收货","采购退厂",
                "调拨出库", "调拨入库","调拨差异",
                "配送出库", "配送入库","配送差异",
                "销售订单","销售出库","销售退货",
                "门店补货","智能补货","实时盘点","录入盘点单","商品资料","商品损溢",};
        dataList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i <icno.length; i++) {
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("img", icno[i]);
            map.put("text",name[i]);
            dataList.add(map);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (dataList.get(i).get("text").toString()) {
            case "门店补货":
                startActivity(SupGoodsListActivity.class);
                break;
            case "调拨出库":
                startActivity(DboOutListActivity.class);
                break;
            case "调拨入库":
                startActivity(DboReceiveListActivity.class);
                break;
            case "调拨差异":
                startActivity(DboCyListActivity.class);
                break;
            case "配送出库":
                startActivity(PsOutListActivity.class);
                break;
            case "配送入库":
                startActivity(PsReceiveListActivity.class);
                break;
            case "配送差异":
                startActivity(PsCyListActivity.class);
                break;
            case "采购订单":
                startActivity(CgshListActivity.class);
                break;
            case "采购收货":
                startActivity(ReceiveBillListActivity.class);
                break;
            case "采购退厂":
                startActivity(RefundBillListActivity.class);
                break;
            case "销售订单":
                startActivity(SaleListActivity.class);
                break;
            case "销售出库":
                startActivity(OutBillListActivity.class);
                break;
            case "销售退货":
                startActivity(com.pospi.xstore.ui.xs.RefundBillListActivity.class);
                break;
            case "商品损溢":
                startActivity(OverFlowListActivity.class);
                break;
            case "实时盘点":
                startActivity(CountsListActivity.class);
                break;
            case "录入盘点单":
                startActivity(PdListActivity.class);
                break;
            case "智能补货":
                startActivity(SupAutoListActivity.class);
                break;
        }
    }

//    @OnClick({R.id.tv_pop, R.id.tv_search})
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.tv_pop:
//                LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//                View contentView = mLayoutInflater.inflate(R.layout.vip_pouple_window, null);
//                // R.layout.pop为PopupWindow 的布局文件
//                pop = new PopupWindow(contentView, TurnSize.dip2px(context, 150), TurnSize.dip2px(context, 180));
//                pop.setBackgroundDrawable(new BitmapDrawable());
//                // 指定 PopupWindow 的背景
//                pop.setFocusable(true);
//                pop.showAsDropDown(tvPop);
//
//                LinearLayout phone = (LinearLayout) contentView.findViewById(R.id.ll_phone);
//                LinearLayout member = (LinearLayout) contentView.findViewById(R.id.ll_vip_member);
//                LinearLayout name = (LinearLayout) contentView.findViewById(R.id.ll_vip_name);
//                LinearLayout scan = (LinearLayout) contentView.findViewById(R.id.ll_vip_scan);
//
//                MyClickListener listener = new MyClickListener();
//                phone.setOnClickListener(listener);
//                member.setOnClickListener(listener);
//                name.setOnClickListener(listener);
//                scan.setOnClickListener(listener);
//
//
//                break;
//            case R.id.tv_search:
//                break;
//        }
//    }

//    class MyClickListener implements View.OnClickListener {
//
//        @Override
//        public void onClick(View v) {
//            switch (v.getId()) {
//                case R.id.ll_phone:
//                    tvPop.setText(Constant.VIP_PHONE);
//                    break;
//                case R.id.ll_vip_member:
//                    tvPop.setText(Constant.VIP_NUMBER);
//                    break;
//                case R.id.ll_vip_name:
//                    tvPop.setText(Constant.VIP_NAME);
//                    break;
//                case R.id.ll_vip_scan:
//                    tvPop.setText(Constant.VIP_SCAN);
//                    break;
//
//            }
//            pop.dismiss();
//        }
//    }
}
